package com.shengyo.model.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.shengyo.model.bean.Digital;
import com.shengyo.model.dao.IDigitalDAO;


public class DigitalDAOImpl extends BaseDAOImpl implements IDigitalDAO{

	@Override
	public void delete(Digital digital) {
		this.getHibernateTemplate().delete(digital);
	}

	@Override
	public Digital findByDigitalUrl(String url) {
		List<?> list = this.getHibernateTemplate().find("from digital b where b.url=?",new Object[]{url});
		if (list != null && !list.isEmpty())
			return (Digital) list.get(0);
		else
			return null;
	}

	@Override
	public Digital findById(String id) {
		return (Digital) this.getHibernateTemplate().load(Digital.class, id);
	}

	@SuppressWarnings("unchecked")
	public Digital findByType(String type) {
		List list = this.getHibernateTemplate().find("from digital d where d.type=?",new Object[]{type});
		if (list !=null && !list.isEmpty())
			return (Digital) list.get(0);
		else
			return null;
	}

	@Override
	public List<?> findAll() {
		return this.getHibernateTemplate().find("from digital");
	}

	@SuppressWarnings("unchecked")
	public List<Digital> findAll(final int first, final int max) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				return session.createQuery("from digital").setFirstResult(first).setMaxResults(max).list();
			}
			
		});
	}

	@Override
	public Object findById(int id) {
		return null;
	}

	/*
	@SuppressWarnings("unchecked")
	public List<Digital> findAll() {
		return this.getHibernateTemplate().find("from digital");
	}

	
	@SuppressWarnings("unchecked")
	public List<Digital> findAll(final int first, final int max) {
		return this.getHibernateTemplate().executeFind(new HibernateCallback(){

			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				return session.createQuery("from digital").setFirstResult(first).setMaxResults(max).list();
			}
			
		});
	}

	
	public Digital findById(int id) {
		return (Digital) this.getHibernateTemplate().load(Digital.class, id);
	}

	
	public void delete(Digital digital) {
		this.getHibernateTemplate().delete(digital);
		
	}

	
	@SuppressWarnings("unchecked")
	public Digital findByType(String type) {
		List list = this.getHibernateTemplate().find("from digital d where d.type=?",new Object[]{type});
		if (list !=null && !list.isEmpty())
			return (Digital) list.get(0);
		else
			return null;
	}
*/
}
